package com.dhl.fin.api.service;

import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.common.util.StringUtil;
import com.dhl.fin.api.common.util.excel.ExcelSheet;
import com.dhl.fin.api.common.util.excel.ExcelUtil;
import com.dhl.fin.api.dao.MyConsultChatDao;
import com.dhl.fin.api.domain.Consult;
import com.dhl.fin.api.domain.ConsultChat;
import com.dhl.fin.api.domain.Family;
import com.dhl.fin.api.enums.ConsultChatStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ConsultChatService extends CommonService<ConsultChat> {

    @Autowired
    private FamilyService familyService;

    @Autowired
    private MyConsultChatDao myConsultChatDao;

    @Autowired
    private ConsultService consultService;


    public List<ConsultChat> getChatData(Long consultId) throws Exception {
        return myConsultChatDao.selectAllConsultChatData(consultId);
    }


    @Override
    public void beforeSave(ConsultChat domain) throws Exception {
        Family family = familyService.getLoginUserInfo();
        if (ObjectUtil.notNull(family)) {
            domain.setOwner(family.getWxAccountCode());
            domain.setOwnerHead(family.getHeadPicPath());
        }
    }

    @Override
    public void afterSave(ConsultChat domain) throws Exception {
        consultService.updateFieldById(domain.getConsult().getId(), "replyStatus", ConsultChatStatusEnum.WAIT_REPLY.getCode());
    }

    public void importConsult(MultipartHttpServletRequest request) throws Exception {
        List<ConsultChat> datas = ExcelUtil.getExcelFromRequest(request, ConsultChat.class);
        for (ConsultChat chat : datas) {

            if (StringUtil.isEmpty(chat.getReplayContent())) {
                continue;
            }

            Long consultId = Long.parseLong(chat.getConsultNum());
            String replayContent = chat.getReplayContent();
            Consult consult = new Consult();
            consult.setId(consultId);
            ConsultChat consultChat = new ConsultChat();
            consultChat.setConsult(consult);
            consultChat.setContent(replayContent);
            consultChat.setOwner("system");

            //新增一条Chat数据
            saveDomain(consultChat);

            //更新Consult为已回复
            consultService.updateFieldById(consultId, "replyStatus", ConsultChatStatusEnum.REPLIED.getCode());

        }
    }

    public void exportConsult(HttpServletResponse response) throws Exception {
        List<ConsultChat> consultChatList = myConsultChatDao.selectAllWaitReplayChat();
        ExcelUtil.builder()
                .addSheet(ExcelSheet.builder()
                        .setTitle(ConsultChat.class)
                        .addRowList(consultChatList)
                        .build())
                .build().writeToClient(response);
    }

}
